package com.blockmeta.bbs.baselibrary.utils;

import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

/**
 * @author by author
 * @date 2019/8/8
 */
public class ImageUtils {
    public static void loadGlide(Context context, String url, int errorPic, ImageView imageView) {
        if (!TextUtils.isEmpty(url)) {
            if (context instanceof AppCompatActivity) {
                if (((AppCompatActivity) context).isFinishing()) {
                    return;
                } else {
                    loadPic(context, url, errorPic, imageView);
                }
            } else {
                loadPic(context, url, errorPic, imageView);
            }
        } else {
            Glide.with(context).load(errorPic).into(imageView);
        }

    }

    private static void loadPic(Context context, String url, int errorPic, ImageView imageView) {
        Glide.with(context).load(url)
                .placeholder(errorPic)
                .error(errorPic)
                .into(imageView);
    }


    public static void loadPicasso(Context context, String url, int errorPic, ImageView imageView) {
        if (!TextUtils.isEmpty(url)) {
            Picasso.with(context).load(url)
                    .error(errorPic)
                    .placeholder(errorPic)
                    .into(imageView);
        } else {
            Picasso.with(context).load(errorPic).into(imageView);
        }
    }
}
